<?php
	class PlayAction extends Common {
		function index(){
			
			$where="";
			$pid=0;
			$play=D("play");
			$this->mess('共有幻灯片 <b class="red_font">'.$play->total().'</b> 张，在首页中最多显示6张');
			$page=new Page($play->total($where), PICTURE_PAGE_SIZE,"pid/{$pid}");
			$page->set("head", "张图片")
			     ->set("first", "|&lt;")
			     ->set("last", "&gt;|")
			     ->set("next", "&gt;&gt;|")
			     ->set("prev", "|&lt;&lt;");
			$plays=$play->field("id,name,water")->order("id desc")->limit($page->limit)->select($where);

			$this->assign("plays", $plays);
			$this->assign("fpage", $page->fpage(0,3,4,5,6));
			$this->assign("pid",$pid);

			$this->display();
		}

		function add() {
			$this->mess('提示: 带<span class="red_font">*</span>的项目为必填信息.<br>注意: 可以上传的图片格式支持GIF,JPEG和PNG三种,大小不能超过2M.  ');
			$this->display();
		}


		function insert(){

			global $pictureSize,$thumbSize;
			$up=new FileUpload();
			if(isset($_POST["thumb"])) {
				$up->set("thumb", array("width"=>$thumbSize["width"], "height"=>$thumbSize["height"]));
				$this->assign("thumb", "checked");
			} else {
				$up->set("thumb", array("width"=>$pictureSize["maxWidth"], "height"=>$pictureSize["maxHeight"]));
			}
			
			if(isset($_POST["water"])){
				$up->set("watermark", array("water"=>WATER, "position"=>POSITION, "prefix"=>"wa_"));
				$this->assign("water", "checked");
			}


			if($up->upload("pic")){
				$play=D("play");
				$names=$up->getFileName();	
				foreach($names as $name){
					$_POST["name"]=$name;
					$play->insert($_POST, 1, 1);
				}
				$this->mess('添加成功.', true);
				$this->redirect("index", "pid/{$_POST["pid"]}");
			}else{
			
				$this->mess($up->getErrorMsg(),false);
				$album=D("album");
				$this->assign("select", $album->formselect("pid", $_POST["pid"], "请选择相册"));
				
				$this->display("add");
			}
			
		}

		function order(){
			$play=D("play");

			for($i=0; $i < count($_POST["ids"]); $i++){
				$play->update(array("id"=>$_POST["ids"][$i],"ord"=>$_POST["ord"][$i]));	
			}

			$this->redirect("index");
		}


		function del(){
			$play=D("play");
			$plays=$play->field("name")->select($_POST["id"]);
			if($play->delete($_POST["id"])) {
				$play->delplay($plays); //删除图片
				$this->redirect("index", "pid/{$_GET["pid"]}");
			}else{
				$this->error("删除幻灯片失败", 1, "play/index/pid/{$_GET["pid"]}");
			}
				
		}


	}
